﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace D2DEngine
{
    public abstract class D2DMaterial : ICloneable
    {
        protected Texture2D texture;
        protected Color color = Color.White;
        protected float scale = 1.0f;

        protected float defaultRotationAngle = 0.0f;

        /// <summary>
        /// The texture's rotation angle as it appear in the image.
        /// The angle needed for the image to face to the right.
        /// 
        /// Ex:
        /// > angle = 0
        /// ^ angle = 90
        /// left angle = 180;
        /// down = -90
        /// etc.
        /// </summary>
        public float DefaultRotationAngle
        {
            get { return this.defaultRotationAngle; }
            set { this.defaultRotationAngle = value; }
        }

        /// <summary>
        /// The scale of the material
        /// </summary>
        public float Scale
        {
            get { return this.scale; }
            set { this.scale = value; }
        }

        /// <summary>
        /// The texture of the material
        /// </summary>
        public Texture2D Texture
        {
            get { return this.texture; }
            set { this.texture = value; }
        }

        /// <summary>
        /// The tint of the material
        /// </summary>
        public Color Color
        {
            get { return this.color; }
            set { this.color = value; }
        }

        /// <summary>
        /// The center of the material
        /// </summary>
        public Vector2 Origin
        {
            get { return new Vector2(Width / 2, Height / 2); }
        }

        /// <summary>
        /// The material's width in pixel
        /// </summary>
        public virtual int Width
        {
            get { return texture.Width; }
        }

        /// <summary>
        /// The material's height in pixel
        /// </summary>
        public virtual int Height
        {
            get { return texture.Height; }
        }

        public virtual Rectangle? SourceRect
        {
            get { return null; }
        }

        /// <summary>
        /// Creates a material
        /// </summary>
        /// <param name="texture">The texture of the material</param>
        public D2DMaterial(Texture2D texture)
        {
            this.texture = texture;
        }

        public D2DMaterial() { }

        /// <summary>
        /// Updates the material
        /// </summary>
        /// <param name="gameTime"></param>
        public virtual void Update(GameTime gameTime)
        {
        }

        /// <summary>
        /// Clones the material
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            D2DMaterial material = (D2DMaterial)Activator.CreateInstance(this.GetType());

            CopyTo(material);

            return material;
        }

        protected virtual void CopyTo(D2DMaterial material)
        {
            material.Texture = this.texture;
            material.Color = this.color;
            material.Scale = this.scale;
            material.DefaultRotationAngle = this.defaultRotationAngle;
        }
    }
}
